﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Xml.Linq;

namespace Components
{
    public class WorkflowTriggerInstructionBase : InstructionBase
    {
        #region props
        /// <summary>
        /// workflow can have maximum of one trigger
        /// all trigger must have associated workflow
        /// </summary>
        public bool IsActive { get; set; }
        public DateTime ActivatedOn { get; set; }
        public TimeSpan Span { get; set; }
        public DateTime? LastRunTime { get; set; }
        public DateTime? NextRunTime { get; set; }
        #endregion

        #region ctor
        public WorkflowTriggerInstructionBase()
            : base(Guid.Empty, Guid.Empty)
        {
            this.IsActive = false;
            this.ActivatedOn = DateTime.Now;
            this.Span = new TimeSpan(1, 0, 0, 0); // every day
            this.LastRunTime = null;
            this.NextRunTime = null;
        }

        public WorkflowTriggerInstructionBase(
            Guid componentID, Guid ticketID,
            bool isActive, DateTime activatedOn,
            TimeSpan span, DateTime? lastRunTime, DateTime? nextRunTime)
            : base(componentID, ticketID)
        {
            this.IsActive = isActive;
            this.ActivatedOn = activatedOn;
            this.Span = span;
            this.LastRunTime = lastRunTime;
            this.NextRunTime = nextRunTime;
        }
        #endregion

        public override XElement Serialize()
        {
            XElement xe = base.Serialize();
            xe.Add(
                new XAttribute("IsActive", this.IsActive.ToString()),
                new XAttribute("ActivatedOn", this.ActivatedOn.ToString()),
                new XAttribute("Span", this.Span.Minutes.ToString()));
            if (this.LastRunTime.HasValue)
            {
                XAttribute lastRunAtt = null;
                lastRunAtt = new XAttribute("LastRunTime", this.LastRunTime.ToString());
                xe.Add(lastRunAtt);
            }
            if (this.NextRunTime.HasValue)
            {
                XAttribute nextRunAtt = null;
                nextRunAtt = new XAttribute("NextRunTime", this.NextRunTime.ToString());
                xe.Add(nextRunAtt);
            }
            return xe;
        }

        public override InstructionBase Instantiate(System.Xml.Linq.XElement xe)
        {
            WorkflowTriggerInstructionBase instruction = (WorkflowTriggerInstructionBase)base.Instantiate(xe);
            instruction.IsActive = bool.Parse(xe.Attribute("IsActive").Value);
            instruction.ActivatedOn = DateTime.Parse(xe.Attribute("ActivatedOn").Value);
            instruction.Span = TimeSpan.FromMinutes(int.Parse(xe.Attribute("Span").Value));
            if (xe.Attribute("LastRunTime") != null)
                instruction.LastRunTime = DateTime.Parse(xe.Attribute("LastRunTime").Value);
            if (xe.Attribute("NextRunTime") != null)
                instruction.NextRunTime = DateTime.Parse(xe.Attribute("NextRunTime").Value);
            return instruction;
        }
    }
}
